package com.jushu.storbox.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.operation.group.TeamspaceOperation;

public class TeamListAdapter extends ListDisplayResolveAdapter<TeamSpaceInfo, TeamspaceOperation<TeamSpaceInfo>> {

	public static final String TAG = TeamListAdapter.class.getSimpleName();
	private LayoutInflater mInflater;
	Context context;

	public TeamListAdapter(Context context, List<TeamSpaceInfo> datasList) {
		super(datasList);
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public void addDatas(List<TeamSpaceInfo> datasList) {
		if (datasList == null) {
			return;
		}
		this.objectList.addAll(datasList);
		notifyDataSetChanged();
	}

	public void removeData(TeamSpaceInfo item) {
		if (item == null) {
			return;
		}
		this.objectList.remove(item);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder(mInflater, parent);
			convertView = holder.baseView;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.bindingData(position, getItem(position));

		return convertView;
	}

	public void selectAll() {

	}

	public void cancelSelect() {

	}

	public class ViewHolder {
		// ui view
		View baseView;
		TextView mItemName;
		TextView mItemOwner;
		TextView mItemMembers;

		// data index
		int mIndex;
		// data
		TeamSpaceInfo mInfo;

		public TeamSpaceInfo getmInfo() {
			return mInfo;
		}

		@SuppressWarnings("deprecation")
		public ViewHolder(LayoutInflater mInflater, ViewGroup parent) {

			// init view
			baseView = mInflater.inflate(R.layout.item_teamlist, parent, false);
			mItemName = (TextView) baseView.findViewById(R.id.item_name_text);
			mItemOwner = (TextView) baseView.findViewById(R.id.item_owner_text);
			mItemMembers = (TextView) baseView.findViewById(R.id.item_members_text);

		}

		public void bindingData(int index, TeamSpaceInfo info) {
			mIndex = index;
			mInfo = info;
			// bindding data
			mItemName.setText(info.getName());
			mItemOwner.setText(context.getString(R.string.team_space_owner)
					+ (info.getOwnerByUserName() != null ? info.getOwnerByUserName() : info.getCreatedByUserName()));
			mItemOwner.getText().toString();
			mItemMembers.setText(context.getString(R.string.team_space_current_numbers) + info.getCurNumbers());
		}

	}

}
